package com.mia.props.common.entities;

import com.mia.props.DecoModelMetadata;
import com.mia.props.Props;
import com.mia.props.network.Msg0x01SelectDeco;
import com.mia.props.network.NetworkHelper;
import java.util.Iterator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;

public class TileDecobench extends TileContainer implements ISidedInventory {
   public final int[] MAX_STORE = new int[]{6000, 2000, 2000, 2000};
   private int[] resources = new int[4];

   public TileDecobench() {
      this.slots = new ItemStack[2];
   }

   public int getResource(int idx) {
      return this.resources[idx];
   }

   public int[] getResources() {
      return this.resources;
   }

   public void setResource(int idx, int amount) {
      this.resources[idx] = amount;
   }

   public void writeToNBT(NBTTagCompound tagCompound) {
      super.writeToNBT(tagCompound);
      tagCompound.setInteger("clay", this.resources[0]);
      tagCompound.setInteger("red", this.resources[1]);
      tagCompound.setInteger("blue", this.resources[2]);
      tagCompound.setInteger("green", this.resources[3]);
   }

   public void readFromNBT(NBTTagCompound tagCompound) {
      super.readFromNBT(tagCompound);
      this.resources[0] = tagCompound.getInteger("clay");
      this.resources[1] = tagCompound.getInteger("red");
      this.resources[2] = tagCompound.getInteger("blue");
      this.resources[3] = tagCompound.getInteger("green");
   }

   public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float var7, float var8, float var9) {
      if (!world.isRemote) {
         player.openGui(Props.instance, 1, world, x, y, z);
      }

      return true;
   }

   public int getSizeInventory() {
      return 2;
   }

   public String getInventoryName() {
      return "decocraft.decobench";
   }

   public void setInventorySlotContents(int slot, ItemStack stack) {
      this.slots[slot] = stack;
   }

   public boolean isItemValidForSlot(int slot, ItemStack stack) {
      return slot != 1 && super.isItemValidForSlot(slot, stack);
   }

   public int[] getAccessibleSlotsFromSide(int side) {
      return new int[]{0, 1};
   }

   public boolean canInsertItem(int slot, ItemStack stack, int side) {
      return slot == 0;
   }

   public boolean canExtractItem(int slot, ItemStack stack, int side) {
      return slot == 1;
   }

   public void onBlockHarvested(World world, int x, int y, int z, int par5, EntityPlayer par6EntityPlayer) {
   }

   public boolean onItemStackDropSaveNBT() {
      return true;
   }

   public static int[] resourcesProvidedByStack(ItemStack stack) {
      int[] resources = new int[4];
      Iterator var2 = Props.allowableResourceItems.iterator();

      label69:
      while(var2.hasNext()) {
         ItemStack allowedStack = (ItemStack)var2.next();
         if (OreDictionary.itemMatches(allowedStack, stack, false)) {
            if (stack.getItem().equals(Props.itemClay)) {
               resources[0] = 2;
               if (stack.getItemDamage() == 2) {
                  resources[2] = 2;
               } else if (stack.getItemDamage() == 1) {
                  resources[3] = 2;
               } else {
                  resources[1] = 2;
               }
            } else if (stack.getItem().equals(Props.itemProps)) {
               if (Props.allowPropRecycling) {
                  DecoModelMetadata meta = (DecoModelMetadata)Props.modelData.get(stack.getItemDamage());

                  for(int i = 0; i < 4; ++i) {
                     resources[i] = meta.resourceCost[i];
                  }
               }
            } else if (stack.getItem().equals(Items.clay_ball)) {
               resources[0] = 2;
            } else if (stack.getItem().equals(Item.getItemFromBlock(Blocks.clay))) {
               resources[0] = 8;
            } else {
               int[] oreIds = OreDictionary.getOreIDs(stack);
               int[] var13 = oreIds;
               int var6 = oreIds.length;
               int var7 = 0;

               while(true) {
                  if (var7 >= var6) {
                     break label69;
                  }

                  int id = var13[var7];

                  try {
                     int[] dyeResources = TileDecobench.DyeResourceMap.valueOf(OreDictionary.getOreName(id)).resources;
                     resources[1] = dyeResources[0];
                     resources[2] = dyeResources[1];
                     resources[3] = dyeResources[2];
                  } catch (Exception var10) {
                  }

                  ++var7;
               }
            }
            break;
         }
      }

      for(int i = 0; i < 4; ++i) {
         resources[i] *= stack.stackSize;
      }

      return resources;
   }

   public boolean roomForResources(int[] resourcesToAdd) {
      return resourcesToAdd[0] + this.resources[0] <= this.MAX_STORE[0] && resourcesToAdd[1] + this.resources[1] <= this.MAX_STORE[1] && resourcesToAdd[2] + this.resources[2] <= this.MAX_STORE[2] && resourcesToAdd[3] + this.resources[3] <= this.MAX_STORE[3];
   }

   public void onData(EntityPlayer player, int decoID, boolean shiftkeyDown) {
      if (this.worldObj.isRemote) {
         NetworkHelper.INSTANCE.network.sendToServer(new Msg0x01SelectDeco(this, decoID, shiftkeyDown));
      } else {
         boolean isDirty = false;
         int[] var10000;
         if (decoID < 0) {
            if (decoID == -1 && this.slots[0] != null) {
               int[] resourcesProvided = resourcesProvidedByStack(this.slots[0]);
               if (shiftkeyDown || this.roomForResources(resourcesProvided)) {
                  for(int i = 0; i < 4; ++i) {
                     var10000 = this.resources;
                     var10000[i] += resourcesProvided[i];
                     if (this.resources[i] > this.MAX_STORE[i]) {
                        this.resources[i] = this.MAX_STORE[i];
                     }
                  }

                  this.slots[0] = null;
                  isDirty = true;
               }
            }
         } else {
            DecoModelMetadata modelmeta = (DecoModelMetadata)Props.modelData.get(decoID);

            while((this.slots[1] == null ? Props.itemProps.getItemStackLimit() : this.slots[1].getMaxStackSize() - this.slots[1].stackSize) > 0 && modelmeta.isCraftable(this.resources)) {
               ItemStack stack = new ItemStack(Props.itemProps, 1, decoID);
               if (this.slots[1] == null || this.slots[1].isItemEqual(stack) && this.slots[1].stackSize < this.slots[1].getMaxStackSize()) {
                  if (this.slots[1] == null) {
                     this.slots[1] = stack.copy();
                  } else {
                     ++this.slots[1].stackSize;
                  }

                  for(int r = 0; r < 4; ++r) {
                     var10000 = this.resources;
                     var10000[r] -= modelmeta.resourceCost[r];
                  }

                  isDirty = true;
               }

               if (!shiftkeyDown) {
                  break;
               }
            }

            if (isDirty) {
               this.markDirty();
            }
         }

      }
   }

   public static enum DyeResourceMap {
      dyeRed(8, 0, 0),
      dyeGreen(0, 8, 0),
      dyeBlue(0, 0, 8),
      dyePurple(4, 0, 4),
      dyeCyan(0, 4, 4),
      dyeYellow(4, 4, 0);

      int[] resources = new int[3];

      private DyeResourceMap(int r, int g, int b) {
         this.resources[0] = r;
         this.resources[1] = g;
         this.resources[2] = b;
      }
   }
}
